
import sai.mysql.DriverLoading;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class DoctorServlet extends HttpServlet {

    PreparedStatement prepare, prepare1;

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        try {
            String doctorname = req.getParameter("dname");            
            String gender = req.getParameter("gender");
            String contact = req.getParameter("mobile");
            String dob1 = contact.substring(contact.length() - 7, contact.length());
            String doctorid = "doc" + dob1;
            String email = req.getParameter("email");
            String username = req.getParameter("username");
            String password = req.getParameter("repassword");
            String experience = req.getParameter("experience");
            String qual = req.getParameter("qual");
            String address = req.getParameter("address");
            String dept = req.getParameter("dept");
            out.println(dept);
            DriverLoading dl = new DriverLoading();
            /* String url1="insert into `logindetails` values('"+
            username+"','"
            +password+"','"+email+"','"+"patient"+"')";*/
            prepare1 = dl.getPreparedStatement("insert into `logindetails` values(?,?,?,?)");
            prepare1.setString(1, username);
            prepare1.setString(2, password);
            prepare1.setString(3, email);
            prepare1.setString(4, "doctor");
            prepare1.executeUpdate();
            /* String url="insert into `patient` values('"
            +patientid+"'"
            + ",'"+
            username+"'"
            + ","
            + "'"+patientname+"'"
            + ",'"+fathername+"'"
            + ",'"+gender+"'"
            + ",'"+dob+"'"
            + ",'"+email+"'"
            + ",'"+status+"'"
            + ",'"+address+"'"
            + ",'"+contact+"',"+null+
            ")";*/
            Statement state = dl.getStatement();
            ResultSet rs = state.executeQuery("select DEPT_ID from `department` where DEPT_NAME='" + dept + "'");
            int deptid = 0;
            while (rs.next()) {
                deptid = rs.getInt(1);
            }
            prepare = dl.getPreparedStatement("insert into `doctor` values(?,?,?,?,?,?,?,?,?,?,?)");
            prepare.setString(1, doctorid);
            prepare.setString(2, username);
            prepare.setInt(3, deptid);
            prepare.setString(4, doctorname);
            prepare.setString(5, gender);
            prepare.setString(6, email);
            prepare.setString(7, address);
            prepare.setString(8, contact);
            prepare.setString(9, experience);
            prepare.setString(10, qual);
            prepare.setString(11, null);
            prepare.executeUpdate();

        } catch (Exception e) {
            out.println("Exception raised" + e);
        }

    }
}